Sensor Fusion for Kinetis MCUs (ISSDK/KSDK version)
approximations.c
Go to the documentation of this file.
1 // Copyright (c) 2014, 2015, 2016, NXP Semiconductors N.V.,
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name of NXP Semiconductors N.V. nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 // DISCLAIMED. IN NO EVENT SHALL NXP SEMICONDUCTORS N.V. BE LIABLE FOR ANY
19 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 //
26 
27 /*! \file approximations.c
28  \brief Math approximations file
29 
30  Significant efficiencies were found by creating a set of trig functions
31  which trade off precision for improved power/CPU performance. Full details
32  are included in Application Note AN5015: Trigonometry Approximations
33 */
34 
35 #include "math.h"
36 #include "stdlib.h"
37 #include "stdint.h"
38 
39 #include "approximations.h"
40 
41 // function returns an approximation to angle(deg)=asin(x) for x in the range -1 <= x <= 1
42 // and returns -90 <= angle <= 90 deg
43 
44 // maximum error is 10.29E-6 deg
45 float fasin_deg(float x)
46 {
47  // for robustness, check for invalid argument
48  if (x >= 1.0F) return 90.0F;
49  if (x <= -1.0F) return -90.0F;
50 
51  // call the atan which will return an angle in the correct range -90 to 90 deg
52  // this line cannot fail from division by zero or negative square root since |x| < 1
53  return (fatan_deg(x / sqrtf(1.0F - x * x)));
54 }
55 
56 // function returns an approximation to angle(deg)=acos(x) for x in the range -1 <= x <= 1
57 // and returns 0 <= angle <= 180 deg
58 
59 // maximum error is 14.67E-6 deg
60 float facos_deg(float x)
61 {
62  // for robustness, check for invalid arguments
63  if (x >= 1.0F) return 0.0F;
64  if (x <= -1.0F) return 180.0F;
65 
66  // call the atan which will return an angle in the incorrect range -90 to 90 deg
67  // these lines cannot fail from division by zero or negative square root
68  if (x == 0.0F) return 90.0F;
69  if (x > 0.0F) return fatan_deg((sqrtf(1.0F - x * x) / x));
70  return 180.0F + fatan_deg((sqrtf(1.0F - x * x) / x));
71 }
72 
73 // function returns angle in range -90 to 90 deg
74 
75 // maximum error is 9.84E-6 deg
76 float fatan_deg(float x)
77 {
78  float fangledeg; // compute computed (deg)
79  int8_t ixisnegative; // argument x is negative
80  int8_t ixexceeds1; // argument x is greater than 1.0
81  int8_t ixmapped; // argument in range tan(15 deg) to tan(45 deg)=1.0
82 #define TAN15DEG 0.26794919243F // tan(15 deg) = 2 - sqrt(3)
83 #define TAN30DEG 0.57735026919F // tan(30 deg) = 1/sqrt(3)
84  // reset all flags
85  ixisnegative = ixexceeds1 = ixmapped = 0;
86 
87  // test for negative argument to allow use of tan(-x)=-tan(x)
88  if (x < 0.0F)
89  {
90  x = -x;
91  ixisnegative = 1;
92  }
93 
94  // test for argument above 1 to allow use of atan(x)=pi/2-atan(1/x)
95  if (x > 1.0F)
96  {
97  x = 1.0F / x;
98  ixexceeds1 = 1;
99  }
100 
101  // at this point, x is in the range 0 to 1 inclusive
102  // map argument onto range -tan(15 deg) to tan(15 deg)
103  // using tan(angle-30deg) = (tan(angle)-tan(30deg)) / (1 + tan(angle)tan(30deg))
104  // tan(15deg) maps to tan(-15 deg) = -tan(15 deg)
105  // 1. maps to (sqrt(3) - 1) / (sqrt(3) + 1) = 2 - sqrt(3) = tan(15 deg)
106  if (x > TAN15DEG)
107  {
108  x = (x - TAN30DEG) / (1.0F + TAN30DEG * x);
109  ixmapped = 1;
110  }
111 
112  // call the atan estimator to obtain -15 deg <= angle <= 15 deg
113  fangledeg = fatan_15deg(x);
114 
115  // undo the distortions applied earlier to obtain -90 deg <= angle <= 90 deg
116  if (ixmapped) fangledeg += 30.0F;
117  if (ixexceeds1) fangledeg = 90.0F - fangledeg;
118  if (ixisnegative) fangledeg = -fangledeg;
119 
120  return (fangledeg);
121 }
122 
123 // function returns approximate atan2 angle in range -180 to 180 deg
124 
125 // maximum error is 14.58E-6 deg
126 float fatan2_deg(float y, float x)
127 {
128  // check for zero x to avoid division by zero
129  if (x == 0.0F)
130  {
131  // return 90 deg for positive y
132  if (y > 0.0F) return 90.0F;
133 
134  // return -90 deg for negative y
135  if (y < 0.0F) return -90.0F;
136 
137  // otherwise y= 0.0 and return 0 deg (invalid arguments)
138  return 0.0F;
139  }
140 
141  // from here onwards, x is guaranteed to be non-zero
142  // compute atan2 for quadrant 1 (0 to 90 deg) and quadrant 4 (-90 to 0 deg)
143  if (x > 0.0F) return (fatan_deg(y / x));
144 
145  // compute atan2 for quadrant 2 (90 to 180 deg)
146  if ((x < 0.0F) && (y > 0.0F)) return (180.0F + fatan_deg(y / x));
147 
148  // compute atan2 for quadrant 3 (-180 to -90 deg)
149  return (-180.0F + fatan_deg(y / x));
150 }
151 
152 // approximation to inverse tan function (deg) for x in range
153 // -tan(15 deg) to tan(15 deg) giving an output -15 deg <= angle <= 15 deg
154 
155 // using modified Pade[3/2] approximation
156 float fatan_15deg(float x)
157 {
158  float x2; // x^2
159 #define PADE_A 96.644395816F // theoretical Pade[3/2] value is 5/3*180/PI=95.49296
160 #define PADE_B 25.086941612F // theoretical Pade[3/2] value is 4/9*180/PI=25.46479
161 #define PADE_C 1.6867633134F // theoretical Pade[3/2] value is 5/3=1.66667
162  // compute the approximation to the inverse tangent
163  // the function is anti-symmetric as required for positive and negative arguments
164  x2 = x * x;
165  return (x * (PADE_A + x2 * PADE_B) / (PADE_C + x2));
166 }
float fatan_deg(float x)
float fasin_deg(float x)
#define TAN30DEG
float fatan_15deg(float x)
Math approximations file.
#define PADE_C
#define TAN15DEG
#define PADE_A
#define PADE_B
float facos_deg(float x)
float fatan2_deg(float y, float x)